import React, { Component } from 'react'
import { ListBox } from '../../../components'
import UpdateWorkareaPage from './UpdateNew'
import UpdateWorkareaPageUp from './UpdateNewUp'
import InfoPage from './Info'
import OpenPage from './Open'
import CloseNewPage from './CloseNew'
import UpdatePage from './Update'
import { fetchData } from '../../../components/AjaxConsumer'
import { Button, Modal, Form, Input, Radio, Upload, Icon } from 'antd';

export default class ListProStroagePage extends Component {
    constructor(props) {
        super(props);
        this.state = {
            tableColumn: {},
            searchComponentData: [],
            visible: false,
        };
        //行操作列表
        this.rowOperation = [

        ];
        //header 操作列表
        this.headerOperation = [
            {
                handleName: 'close',
                showName: '冻结',
                iconClass: 'pushpin',
                permission: 'prostorage-freeze'
            },
            {
                handleName: 'open',
                showName: '解冻',
                iconClass: 'pushpin-o',
                permission: 'prostorage-unfreeze'
            }, {
                handleName: 'modiContact',
                showName: '合同号修改',
                iconClass: 'edit',
                permission: 'pro-changeContact'
            }
            , {
                handleName: 'info',
                showName: '查看',
                iconClass: 'info'
            }
            , {
                handleName: 'export',
                showName: 'Excel',
                iconClass: 'file-excel'
            }
        ];

        //table列表展示列配置
        //showName、columnName 必有属性， columnWidth 可选，默认100, columnHidden: true 列隐藏
        this.tableColumn = {
            storageKey: 'listProStroagePage',
            data: [
                {
                    showName: '合同号',
                    columnName: 'contractNo',
                    columnWidth: 120
                },
                {
                    showName: '型号',
                    columnName: 'model',
                    columnWidth: 130
                }, {
                    showName: '型号描述',
                    columnName: 'modelDesc'
                }, {
                    showName: '型号类型',
                    columnName: 'modelType',
                    columnWidth: 120
                }, {
                    showName: '产品类型',
                    columnName: 'type',
                    showOption: [//页面显示配置
                        {
                            optionKey: '0',
                            optionShow: '正品'
                        }, {
                            optionKey: '1',
                            optionShow: '样品'
                        }
                    ]
                }, {
                    showName: '个数',
                    columnName: 'amount',
                    columnWidth: 70
                }, {
                    showName: '单位',
                    columnName: 'unit',
                    columnWidth: 80
                }, {
                    showName: '库存数量',
                    columnName: 'iventAmount',
                    columnWidth: 95
                }, {
                    showName: '可用数量',
                    columnName: 'availAmount',
                    columnWidth: 95
                }, {
                    showName: '总冻结数量',
                    columnName: 'lockAmount',
                    columnWidth: 100
                },
                // {
                //     showName: '仓库',
                //     columnName: 'whCode'
                // }, {
                //     showName: '区域',
                //     columnName: 'wdCode'
                // }, {
                //     showName: '库区',
                //     columnName: 'waCode'
                // },
                {
                    showName: ' 库位',
                    columnName: 'wlCode'
                }
            ]
        };

        //搜索属性配置
        this.searchComponentData = [
            {
                showName: '型号类型',
                name: 'modelType',
                type: 'select',
                optionList: [

                ]
            }, {
                showName: '产品类型',
                name: 'type',
                type: 'select',
                optionList: [
                    {
                        optionKey: '',
                        optionShow: ''
                    },
                    {
                        optionKey: '0',
                        optionShow: '正品'
                    }, {
                        optionKey: '1',
                        optionShow: '样品'
                    }
                ]
            }, {
                showName: '库存数量',
                name: 'iventAmount',
                type: 'number',
                maxName: 'iventAmountMax', //最大值
                minName: 'iventAmountMin'  //最小值
            },
            {
                showName: '可用数量',
                name: 'availAmount',
                type: 'number',
                maxName: 'availAmountMax', //最大值
                minName: 'availAmountMin'  //最小值
            }, {
                showName: '总冻结数量',
                name: 'lockAmount',
                type: 'number',
                maxName: 'lockAmountMax', //最大值
                minName: 'lockAmountMin'  //最小值
            }

        ];

    }


    componentWillMount() {
        const dicType = 'model_type_p';
        const options = {
            url: '/base/dic/get/' + dicType,
            type: 'get',
            condition: {}
        };
        //先写死，后面再改
        fetchData(this, options, this.dicInitCallBackHandle);
    }

    //下拉框 初始化
    dicInitCallBackHandle = (resData) => {
        //alert('resData' + JSON.stringify(resData, null, 4));
        let optionList = [];
        let tmp = {};
        tmp.optionKey = '';
        tmp.optionShow = '';
        optionList.push(tmp);
        resData.data.map((e, index) => {
            let tmp = {};
            tmp.optionKey = e.dicCode;
            tmp.optionShow = e.dicName;
            optionList.push(tmp);
        });

        //alert('optionList' + JSON.stringify(optionList, null, 4));
        this.tableColumn.data[3].showOption = optionList;
        this.searchComponentData[0].optionList = optionList;
    }


    rowHandles = (strHandles, rowObj) => {
        console.log("strHandles===ListResource=======", strHandles);
        if (strHandles === 'close') {
            this.closeHandle(rowObj);
        }
        else if (strHandles == 'open') {
            this.openHandle(rowObj);
        }
        else if (strHandles == 'doubleClick') {
            this.infoHandle(rowObj);
        }
        else if (strHandles == 'info') {
            this.infoHandle(rowObj);
        }
        else if (strHandles == 'export') {
            this.exportXlsHandle(rowObj);
        }
        else if (strHandles == 'modiContact') {
            this.modiContactHandles(rowObj);
        }
    }

    exportXlsHandle = (conditonData) => {
        console.log('-------导出条件--------', conditonData);
        let param = '?'
        for (let key in conditonData) {
            if (conditonData[key] != '') {
                console.log(key, conditonData[key]);
                param = param + key + '=' + conditonData[key] + '&'
            }
        }
        console.log(param);
        //window.location.href = '/product/insidewarehouse/proStorage/listExp' + param;
        window.open('/product/insidewarehouse/proStorage/listExp' + param)
        //window.showModalDialog('/product/insidewarehouse/proStorage/listExp' + param);
    }

    //
    modiContactHandles = (rowObj) => {
        if (rowObj.length == 0) {
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
            return;
        } else if (rowObj.length > 1) {
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
            return;
        } else if (rowObj[0].data.modelType == 'C-RRT' || rowObj[0].data.modelType == 'C-DUT') {
            Modal.error({
                title: '错误提示',
                content: '记账入库（热熔套、堵头）不能修改合同号！',
            });
        } else {
            this.refs.updateWorkarea.editHandle(rowObj);
        }
    }
    closeHandle = (rowObj) => {
        if (rowObj.length == 0) {
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
            return;
        } else if (rowObj.length > 1) {
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
            return;
        } else {
            let pstData = {};
            pstData.model = rowObj[0].data.model;
            pstData.modelType = rowObj[0].data.modelType;
            pstData.wlCode = rowObj[0].data.wlCode;
            pstData.contractNo = rowObj[0].data.contractNo;
            pstData.type = rowObj[0].data.type;
            pstData.barcodeStatus = '0';
            const options = {
                url: '/product/insidewarehouse/storagesn/r/detail',
                type: 'get',
                condition: pstData
            };
            fetchData(this, options, (resData) => {
                this.refs.closeNewPage.editHandle(resData, rowObj[0].data.RawStorageId,
                    pstData.wlCode, pstData.contractNo, pstData.model, pstData.modelType, pstData.barcodeStatus);
            });
        }
    };
    openHandle = (rowObj) => {
        if (rowObj.length == 0) {
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
            return;
        } else if (rowObj.length > 1) {
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
            return;
        } else {
            let pstData = {};
            pstData.model = rowObj[0].data.model;
            pstData.modelType = rowObj[0].data.modelType;
            pstData.wlCode = rowObj[0].data.wlCode;
            pstData.contractNo = rowObj[0].data.contractNo;
            pstData.barcodeStatus = '1';
            console.log("rowObj[0].data.RawStorageId:****************" + rowObj[0].data.RawStorageId);
            const options = {
                url: '/product/insidewarehouse/storagesn/r/detail',
                type: 'GET',
                condition: pstData,
            };
            fetchData(this, options, (resData) => {
                this.refs.openPage.editHandle(resData, rowObj[0].data.RawStorageId,
                    pstData.wlCode, pstData.contractNo, pstData.model, pstData.modelType, pstData.barcodeStatus);
            });
        }
    };
    infoHandle = (rowObj) => {
        if (rowObj.length == 0) {
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
            return;
        } else if (rowObj.length > 1) {
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
            return;
        } else {
            let pstData = {};
            pstData.wlCode = rowObj[0].data.wlCode;
            pstData.contractNo = rowObj[0].data.contractNo;
            pstData.model = rowObj[0].data.model;
            pstData.modelType = rowObj[0].data.modelType;
            const options = {
                url: '/product/insidewarehouse/storagesn/r/detail',
                type: 'GET',
                condition: pstData,
            };
            fetchData(this, options, (resData) => {
                this.refs.infoPage.editHandle(resData, pstData);
            });

            //this.refs.listDetail.editHandle(rowObj[0].data);
        }
    };

    //刷新列表
    refreshHandle = () => {
        this.refs.listWorkarea.refreshHandle();
    }



    handleCancel = () => {
        this.setState({ visible: false });
        this.form.resetFields();

    }
    handleCreate = () => {
        const form = this.form;
        form.validateFields((err, values) => {
            if (err) {
                return;
            }

            console.log('Received values of form: ', values);
            form.resetFields();
            this.setState({ visible: false });
        });
    }
    saveFormRef = (form) => {
        this.form = form;
    }


    render() {
        //当左侧隐藏时，列表宽度加上左侧的宽度，
        //1、当前this.props.isHideLeft 是从页面组件containers/Main/main.js传过来
        //2、this.props.isHideLeft 最顶级赋值是在页面 containers/Main/main.js
        let isHideLeft = this.props.isHideLeft;

        return (
            <div>
                <ListBox
                    ref='listWorkarea'
                    title="成品库存查询"
                    isHideLeft={isHideLeft}
                    tableColumnData={this.tableColumn}
                    searchComponentData={this.searchComponentData}
                    url='/product/insidewarehouse/storage/list'
                    rowHandles={this.rowHandles}
                    rowOperation={this.rowOperation}
                    headerOperation={this.headerOperation}
                    isRowSingle="true"
                />

                <UpdateWorkareaPage
                    ref="updateWorkarea"
                    refreshHandle={this.refreshHandle}
                />
                <UpdateWorkareaPageUp
                    ref="updateWorkareaUp"
                    refreshHandle={this.refreshHandle}
                />
                <OpenPage
                    ref="openPage"
                    refreshHandle={this.refreshHandle}
                />

                <CloseNewPage
                    ref="closeNewPage"
                    refreshHandle={this.refreshHandle}
                />
                <InfoPage
                    ref="infoPage"
                    refreshHandle={this.refreshHandle}
                />
            </div>
        )
    }
}
